package com.juyu.miliao.scheduler.dao.redis;

import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;


@Repository
public class UserRedisDao {
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	
	public Optional<Integer> getUserIdByUserName(String username){
		Object o = stringRedisTemplate.opsForHash().get("username", username);
		Integer userId = null;
		if (o != null) {
			userId = Integer.parseInt(o.toString());
		}
		return Optional.ofNullable(userId);
	}
	
	public void addUserName(String username, int userId){
		stringRedisTemplate.opsForHash().put("username", username, userId + "");
	}
	
	public void addUserIdAndOpenId(int userId, String openId){
		stringRedisTemplate.opsForHash().put("openId", openId, userId + "");
	}
	
	public Optional<Integer> getUserIdByOpenId(String openId){
		Integer userId = (Integer) stringRedisTemplate.opsForHash().get("openId", openId);
		return Optional.ofNullable(userId);
	}

	/**
	 * @param nickName
	 * @return
	 */
	public Optional<Integer> getUserIdByNickName(String nickName) {
		Object o = stringRedisTemplate.opsForHash().get("nickname", nickName);
		Integer userId = null;
		if (o != null) {
			userId = Integer.parseInt(o.toString());
		}
		return Optional.ofNullable(userId);
	}

	/**
	 * @param userId
	 * @param nickname
	 */
	public void addUserIdAndNickName(int userId, String nickname) {
		stringRedisTemplate.opsForHash().put("nickname", nickname, userId+"");
	}
}
